package org.darkforest.merchant.controller;

import org.apache.commons.lang3.StringUtils;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.EnumParamMapper;
import org.darkforest.model.EnumParam;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2017/4/19.
 */
@Controller
@RequestMapping(value = "enumParam")
public class EnumParamController {



    @Autowired
    private EnumParamMapper enumParamMapper;

    /**
     *
     * @param paramKey 参数键
     * @param paramValue 参数值
     * @param p 当前页
     * @return
     */
    @RequestMapping(value = "list")
    public ModelAndView list(String paramKey,String paramValue,String paramType,
                             @RequestParam(required = false, defaultValue = "1") int p){
        EnumParam enumParam = new EnumParam();
        enumParam.setStart(p);
        enumParam.setParamKey(StringUtils.isNotBlank(paramKey) ? paramKey : null);
        enumParam.setParamValue(StringUtils.isNotBlank(paramValue) ? paramValue : null);
        enumParam.setParamType(StringUtils.isNotBlank(paramType) ? paramType : null);
        List<EnumParam> enumParamList = enumParamMapper.selectListByCondition(enumParam);
        enumParam.setResultList(enumParamList);
        ModelAndView res = new ModelAndView("param/enum_param_list");
        res.addObject("pageBean",enumParam);
        return res;
    }


    @RequestMapping(value = "delEnumParam")
    public @ResponseBody boolean delEnumParam(Integer paramId){
        enumParamMapper.deleteByPrimaryKey(paramId);
        return true;
    }

    @RequestMapping(value = "toModifyParam")
    public ModelAndView toModifyParam(Integer paramId){
        ModelAndView res = new ModelAndView("param/param_edit");
        res.addObject("enumParam", enumParamMapper.selectByPrimaryKey(paramId));
        return res;
    }

    @RequestMapping(value = "doModifyParam")
    public @ResponseBody boolean doModifyParam(EnumParam enumParam){

        enumParamMapper.updateByPrimaryKeySelective(enumParam);
        return true;
    }


    @RequestMapping(value = "toAddEnumParam")
    public ModelAndView toAddEnumParam(){
        ModelAndView res = new ModelAndView("param/param_add");
        return res;
    }


    @RequestMapping(value = "doAddEnumParam")
    public @ResponseBody boolean doAddEnumParam(EnumParam enumParam,HttpServletRequest request){
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        enumParam.setCreateName(sellerVo.getMemberName());
        enumParam.setCreateTime(DateUtil.getCurrentDateTime());
        enumParam.setCreaterId(sellerVo.getMemberId());
        enumParamMapper.insertSelective(enumParam);

        return true;
    }


}
